﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Threading;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.Globalization;
using System.Data.SqlClient;

namespace _4591BRD
{
    class Common
    {
        private static object counter = new object();
        private static int _threadCount = 0;
        public static void AddThreadCount()
        {
            lock (counter)
            {
                _threadCount++;
            }
        }
        public static void RemoveThreadCount()
        {
            lock (counter)
            {
                _threadCount--;
            }
        }
        public static int GetThreadCount()
        {
            return _threadCount;
        }
 
        public static bool IsConsole = true;

        public static string BRDLogPath = ConfigurationSettings.AppSettings["BRDPath"].ToString();
        public static string WebDataPath = ConfigurationSettings.AppSettings["WebDataPath"].ToString();

        public static string ConnStrBRD = ConfigurationSettings.AppSettings["ConnStringBRD"].ToString();
        public static string ConnStrTY = ConfigurationSettings.AppSettings["ConnStringTY"].ToString();

        public static bool OnlyMakeRank
        {
            get
            {
                bool ret = false;
                bool.TryParse(ConfigurationSettings.AppSettings["OnlyMakeRank"].ToString(), out ret);
                return ret;
            }
        }
        private static object monitor = new object();

        public static void CheckDataTables(DateTime date)
        {
            lock (monitor)
            {
                try
                {
                    //检查并创建表格
                    SqlParameter[] parameters = { new SqlParameter("@LogDate", SqlDbType.DateTime) };
                    parameters[0].Value = date;
                    SqlHelper.ExecuteNonQuery(Common.ConnStrBRD, CommandType.StoredProcedure, "prCheckDataTables", parameters);
                }
                catch (SqlException e)
                {
                    Log.WriteError(e.Message);
                }
            }
        }

        //执行BCP命令
        public static void ExeCommand(string commandText)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            string strOutput = null;

            try
            {
                p.Start();
                p.StandardInput.WriteLine(commandText);
                p.StandardInput.WriteLine("exit");
                strOutput = p.StandardOutput.ReadToEnd();
                p.WaitForExit();
                p.Close();
            }
            catch (IOException e)
            {
                Log.WriteError(e.Message);
            }
            catch (Exception e)
            {
                Log.WriteError(e.Message);
            }
        }
    }

}
